package com.ttdevs.ttrss.ui.left;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ttdevs.ttrss.R;
import com.ttdevs.ttrss.bean.Source;

public class SourceExpandAdapter extends BaseExpandableListAdapter {

	private List<List<Source>> mList = null;
	private List<String> mGroupNameList = null;
	private LayoutInflater mInflater = null;

	public SourceExpandAdapter(Context ctx, List<List<Source>> list, List<String> groupNameList) {
		mList = list;
		mGroupNameList = groupNameList;
		// mInflater = (LayoutInflater)
		// ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mInflater = LayoutInflater.from(ctx);
	}

	@Override
	public int getGroupCount() {
		return mList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// if (mList.size() > 0 && mList.get(groupPosition).size() > 0) {
		// return mList.get(groupPosition).size();
		// }
		if (mList.get(groupPosition) != null) {
			return mList.get(groupPosition).size();
		}
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		List<Source> data = mList.get(groupPosition);
		return data;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		String data = mList.get(groupPosition).get(childPosition).getTitle();
		return data;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.eplist_item_group, null);
		}
		GroupViewHolder holder = new GroupViewHolder();
		holder.groupIcon = (ImageView) convertView.findViewById(R.id.ivGroupIndicator);

		holder.groupName = (TextView) convertView.findViewById(R.id.tvGroupName);
		holder.groupName.setText(mGroupNameList.get(groupPosition));

		holder.groupCount = (TextView) convertView.findViewById(R.id.tvGroupCount);
		holder.groupCount.setText("[" + mList.get(groupPosition).size() + "]");
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.eplist_item_child, null);
		}
		ChildViewHolder holder = new ChildViewHolder();
		holder.sourceIcon = (ImageView) convertView.findViewById(R.id.ivGroupIndicator);
		holder.sourceTitle = (TextView) convertView.findViewById(R.id.tvSourceTitle);
		holder.sourceLink = (TextView) convertView.findViewById(R.id.tvSourceLink);

		holder.sourceTitle.setText(mList.get(groupPosition).get(childPosition).getTitle());
		holder.sourceLink.setText(mList.get(groupPosition).get(childPosition).getLink());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	private class GroupViewHolder {
		ImageView groupIcon;
		TextView groupName;
		TextView groupCount;
	}

	private class ChildViewHolder {
		ImageView sourceIcon;
		TextView sourceTitle;
		TextView sourceLink;
	}
}
